﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using System.Windows;
using System.Windows.Controls;
using static FruitManagerApp.TreeViewData;

namespace FruitManagerApp
{
    public class ParentWindowViewModel : NotificationObject
    {
        //每个窗口Study都包含独立的窗口
        private String gCurrentUID = null;
        ParentWindow gParentWindow = null;

        public ParentWindowViewModel(Window pWindow)
        {
            gParentWindow = (ParentWindow)pWindow;

            List<CStudy> studyList = PermissionList.GetInstance().GetStudies();
            foreach (CStudy study in studyList)
            {
                var treeNode1 = new TreeNode() { Label = study.Title, Level = 1 };
                TreeViewData.Data.RootNodes.Add(treeNode1);
                List<CSeries> seriesList = PermissionList.GetInstance().GetSerieses(study.StudyUID);
                foreach (CSeries series in seriesList)
                {
                    List<CImage> imageList = PermissionList.GetInstance().GetImages(series.SeriesUID);
                    var treeNode2 = new TreeNode() { Label = series.Title, UID= imageList[0].ImageUID, Level = 2 };
                    treeNode1.ChildNodes.Add(treeNode2);
                }
            }

            var treeNode = new TreeNode() { Label = "个人设置", UID="Settings", Level = 1 };
            TreeViewData.Data.RootNodes.Add(treeNode);
            treeNode = new TreeNode() { Label = "退 出", UID = "Quit", Level = 1 };
            TreeViewData.Data.RootNodes.Add(treeNode);

            Name = App.LoginInfo.Name;
        }

        public void RemoveUserControl(string pSeriesUID)
        {
            ESeriesUID seriesUID = (ESeriesUID)Convert.ToInt16(pSeriesUID);
            gParentWindow.ImagePanel.Children.Clear();

            InitNavigation(PermissionList.GetInstance().GetStudyUID(seriesUID), ESeriesUID.None);
        }

        private String name;
        public String Name
        {
            get { return name; }
            set
            {
                name = value;
                RaisePropertyChanged("Name");
            }
        }

        public void InitNavigation(EStudyUID pEStudyUID, ESeriesUID pESeriesUID)
        {
            if (pEStudyUID == EStudyUID.None)
            {
                gParentWindow.StudyName.Text = "";
            }
            else
            {
                gParentWindow.StudyName.Text = PermissionList.GetInstance().GetStudy(pEStudyUID).Title;
            }
            gParentWindow.StudyName.Uid = ((int)pEStudyUID).ToString();

            if (pESeriesUID == ESeriesUID.None)
            {
                gParentWindow.SeriesName.Content = "";
            }
            else
            {
                gParentWindow.SeriesName.Content = PermissionList.GetInstance().GetSeries(pESeriesUID).Title;
            }
            gParentWindow.SeriesName.Uid = ((int)pESeriesUID).ToString();
        }

        protected DelegateCommand<object> naviStudyCommand;
        public DelegateCommand<object> NaviStudyCommand
        {
            get
            {
                return naviStudyCommand ?? (naviStudyCommand = new DelegateCommand<object>(ExecuteNaviStudyCommand, CanExecuteNaviStudyCommand));
            }
        }

        private void ExecuteNaviStudyCommand(object obj)
        {
            RemoveUserControl(gParentWindow.SeriesName.Uid);
        }

        private bool CanExecuteNaviStudyCommand(object arg)
        {
            return true;
        }

        protected DelegateCommand<object> treeClickCommand;
        public DelegateCommand<object> TreeClickCommand
        {
            get
            {
                return treeClickCommand ?? (treeClickCommand = new DelegateCommand<object>(ExecuteTreeClickCommand));
            }
        }

        private void ExecuteTreeClickCommand(object obj)
        {
            String UID = ((TreeNode)((TreeView)obj).SelectedItem).UID;

            if (UID == "Quit")
            {
                if (MessageBoxResult.Yes == MessageBox.Show("您确定要退出吗?", "退 出", MessageBoxButton.YesNo))
                {
                    gParentWindow.Close();
                }
            }

            if (gCurrentUID == UID) return;

            UserControl window = null;

            if (UID == EImageUID.PersonnelManage)
            {
                window = new Personnel();
                PersonnelViewModel viewModel = new PersonnelViewModel(window, this);
                window.DataContext = viewModel;
            }
            else if (UID == EImageUID.PersonnelGroupManage)
            {
                window = new PersonnelGroup();
                PersonnelGroupViewModel viewModel = new PersonnelGroupViewModel(window, this);
                window.DataContext = viewModel;
            }
            else if (UID == EImageUID.DeviceManage)
            {
                window = new Device();
                DeviceViewModel viewModel = new DeviceViewModel(window, this);
                window.DataContext = viewModel;
            }
            else if (UID == EImageUID.DeviceTypeManage)
            {
                window = new DeviceType();
                DeviceTypeViewModel viewModel = new DeviceTypeViewModel(window, this);
                window.DataContext = viewModel;
            }
            else if (UID == EImageUID.OfficeOfficeManage)
            {
                window = new Office();
                OfficeViewModel viewModel = new OfficeViewModel(window, this);
                window.DataContext = viewModel;
            }
            else if (UID == EImageUID.OfficeCityManage)
            {
                window = new City();
                CityViewModel viewModel = new CityViewModel(window, this);
                window.DataContext = viewModel;
            }
            else if (UID == EImageUID.OfficeProvinceManage)
            {
                window = new Province();
                ProvinceViewModel viewModel = new ProvinceViewModel(window, this);
                window.DataContext = viewModel;
            }
            else if (UID == EImageUID.UserManage)
            {
                window = new User();
                UserViewModel viewModel = new UserViewModel(window, this);
                window.DataContext = viewModel;
            }
            else if (UID == EImageUID.UserVIP)
            {
                window = new VIP();
                VIPViewModel viewModel = new VIPViewModel(window, this);
                window.DataContext = viewModel;
            }
            else if (UID == EImageUID.UserCoupon)
            {
                window = new Coupon();
                CouponViewModel viewModel = new CouponViewModel(window, this);
                window.DataContext = viewModel;
            }
            else if (UID == EImageUID.UserIntegral)
            {

            }
            else if (UID == EImageUID.DominoInsert)
            {

            }
            else if (UID == EImageUID.DominoManage)
            {

            }
            else if (UID == EImageUID.DominoTypeManage)
            {

            }
            else if (UID == EImageUID.DominoParams)
            {

            }
            else if (UID == EImageUID.OrderInsert)
            {

            }
            else if (UID == EImageUID.OrderManage)
            {

            }
            else if (UID == EImageUID.MessageInsert)
            {

            }
            else if (UID == EImageUID.MessageManage)
            {

            }
            else if (UID == EImageUID.WordInsert)
            {

            }
            else if (UID == EImageUID.WordManage)
            {

            }
            else if (UID == EImageUID.ImageInsert)
            {

            }
            else if (UID == EImageUID.ImageManage)
            {

            }

            else if (UID == "Settings")
            {

            }

            if (window != null)
            {
                gParentWindow.ImagePanel.Children.Clear();
                gParentWindow.ImagePanel.Children.Add(window);
                gCurrentUID = UID;
            }
        }
    }
}
